package com.tjbklx1.redis.chap03;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import com.tjbklx1.redis.JedisUtil;

/**
 * 3.7 事务 pipeline
 * @author tjbklx1
 * @date 2018-12-1
 */
public class TransactionApp extends Thread {
	private final static String key ="transaction_key";

	public void run() {
//		System.out.println(Thread.currentThread().getName() + " 启动运行");
		Jedis redis = JedisUtil.getJedis();
		Pipeline pipeline = redis.pipelined();
		pipeline.multi();
		pipeline.incr(key);
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
		}
		pipeline.decrBy(key,2);
		pipeline.exec();
		pipeline.syncAndReturnAll();
	}

	public static void main(String[] args) throws InterruptedException {
		System.out.println(" main start");
		for (int i = 0; i < 10; i++) {
			TransactionApp threadA = new TransactionApp();
			threadA.start();
		}
		Thread.sleep(3000);
		Jedis redis = JedisUtil.getJedis();
		System.out.println(redis.get(key));
	}

}